/*
 * @Filename: UserDaoImpl.java
 * @CreateTime: 2015-11-13 14:16:44
 * @Version: 1.0
 * @All rights (are) reserved.
 * @author miklechun
 */
package com.wonders.qxmanager.dao.impl;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.wonders.qxmanager.base.BaseDao;
import com.wonders.qxmanager.base.WidgetPage;
import com.wonders.qxmanager.dao.TeamDao;
import com.wonders.qxmanager.model.Team;
import com.wonders.qxmanager.model.User;
import com.wonders.qxmanager.utils.PageArrayList;

/**
 * user dao实现
 */
@Repository
public class TeamDaoImpl extends BaseDao<Team, Serializable> implements TeamDao {

	/**
	 * 新增对象
	 * 
	 * @param team
	 */
	@Override
	public void addTeam(Team team) {
		save(team);
	}

	/**
	 * 修改对象
	 * 
	 * @param user
	 */
	@Override
	public void updateTeam(Team team) {
		saveOrUpdate(team);
	}

	/**
	 * 删除对象
	 * 
	 * @param id
	 */
	public boolean deleteTeam(int id) {
		
		return deleteById(id);
	}

	/**
	 * 动态条件 分页查询
	 * 
	 * @param user
	 * @param page
	 */
	@Override
	public PageArrayList<Team> getListByPage(Team team, WidgetPage page) {
		Criteria criteria = getSession().createCriteria(Team.class);
		if (team.getName() != null)
			criteria.add(Restrictions.like("name", "%" + team.getName()
					+ "%"));
		int total = ((Long) criteria.setProjection(Projections.rowCount())
				.uniqueResult()).intValue();
		criteria.setResultTransformer(Criteria.ROOT_ENTITY);
		criteria.setProjection(null);
		int startIndex = page.getStartOfPage();
		criteria.setFirstResult(startIndex - 1);
		criteria.setMaxResults(page.getPageSize());
		List<Team> list = criteria.list();
		PageArrayList<Team> pageList = new PageArrayList<Team>(total,
				page.getPageSize(), page.getCurrentPage());
		pageList.addAll(list);
		return pageList;
	}

	/**
	 * 获取所有对象
	 */
	@Override
	public List<Team> getAll() {
		return getAll();
	}

	/**
	 * 根据ID获取单个对象
	 * 
	 * @param id
	 */
	@Override
	public Team getTeamById(BigDecimal id) {
		return get(id);
	}

	@Override
	public List<Team> getAll(String sqlString) {
		return null;
	}

}
